package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.CommonCofig;
import co.yixiang.modules.system.service.CommonCofigService;
import co.yixiang.modules.system.dto.queryCriteria.CommonCofigQueryCriteria;
import co.yixiang.modules.system.dto.CommonCofigDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author db
* @date 2021-08-19
*/
@AllArgsConstructor
@Api(tags = "文案配置管理")
@RestController
@RequestMapping("/api/commonCofig")
public class CommonCofigController {

    private final CommonCofigService commonCofigService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','commonCofig:list')")
    public void download(HttpServletResponse response, CommonCofigQueryCriteria criteria) throws IOException {
        commonCofigService.download(Convert.toList(CommonCofigDto.class, commonCofigService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询文案配置")
    @ApiOperation("查询文案配置")
    @PreAuthorize("@el.check('admin','commonCofig:list')")
    public ResponseEntity<Object> getCommonCofigs(CommonCofigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(commonCofigService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增文案配置")
    @ApiOperation("新增文案配置")
    @PreAuthorize("@el.check('admin','commonCofig:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CommonCofig resources){
        return new ResponseEntity<>(commonCofigService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改文案配置")
    @ApiOperation("修改文案配置")
    @PreAuthorize("@el.check('admin','commonCofig:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CommonCofig resources){
        commonCofigService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除文案配置")
    @ApiOperation("删除文案配置")
    @PreAuthorize("@el.check('admin','commonCofig:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        commonCofigService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}